<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Message_model extends Model
{
	function Message_model()
	{
		parent::Model();
	}
	
	function get_all_group()
	{
		$data = array();
		mysqli_multi_query($this->db->conn_id, "CALL selfserving_admin_get_all_group();") or show_custom_error(mysqli_error($this->db->conn_id));
		$result = mysqli_store_result($this->db->conn_id);
		while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
		mysqli_free_result($result);
		if(mysqli_more_results($this->db->conn_id))
		{
			mysqli_next_result($this->db->conn_id);
		}
		return $data;
	}
	
	function get_list_message_user($type, $groupId)
	{
		$data = array();
		mysqli_multi_query($this->db->conn_id, "CALL selfserving_admin_message_get_all_user('$type', '$groupId')") or show_custom_error(mysqli_error($this->db->conn_id));
		$result = mysqli_store_result($this->db->conn_id);
		while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
		mysqli_free_result($result);
		if(mysqli_more_results($this->db->conn_id))
		{
			mysqli_next_result($this->db->conn_id);
		}
		return $data;
	}
	
	function inbox_add($reply_id, $from_user_id, $to_user_id, $title, $content, $status, $deleted)
	{
		mysqli_multi_query($this->db->conn_id, "CALL selfserving_admin_inbox_add('$reply_id', '$from_user_id', '$to_user_id', '$title', '$content', '$status', '$deleted');") or show_custom_error(mysqli_error($this->db->conn_id));
		if(mysqli_more_results($this->db->conn_id))
		{
			mysqli_next_result($this->db->conn_id);
		}
	}
	
	function mail_queue_add($templateId, $param, $receive, $cc, $toId)
	{
		mysqli_multi_query($this->db->conn_id, "CALL selfserving_mail_queue_add('$templateId', '$param', '$receive', '$cc', '$toId');") or show_custom_error(mysqli_error($this->db->conn_id));
		if(mysqli_more_results($this->db->conn_id))
		{
			mysqli_next_result($this->db->conn_id);
		}
	}
	
	function mess_search_user($type, $groupId, $stype, $keyword, $keyword_en, $cPage, $num)
	{
		$data = array();
		$data['lstUser'] = array();
		$data['iCount'] = 0;
		$data['pCount'] = 0;
		$start = ($cPage - 1) *  $num;
		mysqli_multi_query($this->db->conn_id, "CALL selfserving_admin_mess_search_user('$type', '$groupId', '$stype', '$keyword', '$keyword_en','$start', '$num');") or show_custom_error(mysqli_error($this->db->conn_id));
		$result = mysqli_store_result($this->db->conn_id);
		while($row = mysqli_fetch_array($result))
		{
			$data['lstUser'][] = $row;
		}
		mysqli_free_result($result);
		
		mysqli_next_result($this->db->conn_id);
		$result = mysqli_store_result($this->db->conn_id);
		$row = mysqli_fetch_array($result);
		$data['iCount'] = $row['num'];
		$data['pCount'] = ($data['iCount'] > $num) ? ceil($data['iCount']/$num) : 1;
		mysqli_free_result($result);
		if(mysqli_more_results($this->db->conn_id))
		{
			mysqli_next_result($this->db->conn_id);
		}
		
		return $data;
	}
	
	function mess_get_list_user_by_uid($strUid)
	{
		$data = array();
		mysqli_multi_query($this->db->conn_id, "CALL selfserving_admin_mess_get_user_by_list_uid('$strUid')") or show_custom_error(mysqli_error($this->db->conn_id));
		$result = mysqli_store_result($this->db->conn_id);
		while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
		mysqli_free_result($result);
		if(mysqli_more_results($this->db->conn_id))
		{
			mysqli_next_result($this->db->conn_id);
		}
		return $data;
	}
 }